//app.js
App({
  onLaunch: function (options) {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
   // this.getsession();
    console.log(options.scene)
    if (options.scene == 1037 || options.scene == 1038 ){
      wx.setStorageSync('session', options.referrerInfo.extraData.session)
      var session = options.referrerInfo.extraData.split('"')[3]
      wx.setStorageSync('session', session)
    }
  },
  onShow(options){
    this.gettotnum()
    this.getsession(function(){})
    if (options.scene == 1037 || options.scene == 1038) {
      // console.log(options)
      // console.log(options.query.session)
      // if (options.query.session){
      //   wx.setStorageSync('session', options.query.session)
      // }else{
      //   wx.setStorageSync('session', options.referrerInfo.extraData.session)
      // }
      // console.log('最后本地session' + wx.getStorageSync('session'))

    }
  },
  globalData: {
    appid: 'wxb88938bab9d81ef4',
    userInfo: null,
    rootUrl:'https://yxy.mall.api.alorvalley.com',
    orderUrl: 'https://mine.api.logic.alorvalley.com',
    url:{
      type:'',
      id:''
    },
    returnData: null,
  },
    setstor(key, value) {
    wx.setStorage({
      key: key,
      data: value
    })
  },
  getsession(cb){
    var that = this
    var returnData = {}
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.info("code", res.code)
        var code = res.code;
        if (code) {
          // 获取用户信息
          wx.getSetting({
            success: res => {
              console.log(res)
              if (res.authSetting['scope.userInfo']) {
                // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                wx.getUserInfo({
                  success: res => {
                    // 可以将 res 发送给后台解码出 unionId
                    this.globalData.userInfo = res.userInfo
                    console.info("前台用户信息", res.userInfo)

                    var iv = res.iv;
                    var encryptedData = res.encryptedData;

                    wx.request({
                      url: that.globalData.orderUrl+'/api/v2/oauth/wechat/oauth',
                      data: {
                        code: code,
                        iv: iv,
                        encryptedData: encryptedData,
                        appid: that.globalData.appid
                      },
                      success: function (res) {
                        console.error(res.data.data[0].moblie)
                          wx.setStorage({
                            key: 'session',
                            data: res.data.data[0].token
                          }),
                            wx.setStorage({
                            key: 'moblie',
                            data: res.data.data[0].moblie
                            })
                          that.globalData.returnData = res
                          typeof cb == "function" && cb(that.globalData.returnData)
                      }
                    })

                    // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                    // 所以此处加入 callba况
                    if (this.userInfoReadyCallback) {
                      this.userInfoReadyCallback(res)
                    }
                  }
                })
              }
            },
            fail: function () {
              console.log('微信用户未授权')
            }
          })
        }
        else {
          console.log('登陆失败')
        }
      }
    })
  },
  islogin(cb,cb2) {
    var that=this
    var returnData = {}
    wx.getStorage({
      key: "session",
      success: function (res) {
        console.error(res)
        that.globalData.returnData = res
        typeof cb == "function" && cb(that.globalData.returnData)
      },
      fail:function(res){
        that.globalData.returnData = res
        typeof cb2 == "function" && cb2(that.globalData.returnData)
      }
    })
  },
  gettotnum(){
    var shop_pro = wx.getStorageSync('shop_pro')
    if (wx.getStorageSync('shop_pro') == '') {
      shop_pro = []
    }
    if (shop_pro.length!=0){
      // wx.showTabBarRedDot(2)
 var num=0;
 for (let item of shop_pro){
   num = num+ item.pronum
 }
 console.log(num)
      wx.setTabBarBadge({
        index: 2,
        text: num.toString()
      })
    }
    else{
      wx.hideTabBarRedDot({index:2})
    }
  },
  getstor(key, cb) {
    var that = this;
    var returnData = {}
    wx.getStorage({
      key: key,
      success: function (res) {
        that.globalData.returnData = res
        typeof cb == "function" && cb(that.globalData.returnData)
      }
    })
  },
  userLogin(){
    var that =this
    wx.checkSession({
      success: function () {
        console.log('checkSession success')
      },
      fail: function () {
        that.getsession(function(){})
      }
    })},

  req(method, url, data, cb) {
    var that = this;
    var returnData = {}
    wx.request({
      header: {
        'content-type': "application/x-www-form-urlencoded",
      },
      method: method,
      url: that.globalData.rootUrl+url,
      data: data,
      success: function (res) {
        that.globalData.returnData = res
        typeof cb == "function" && cb(that.globalData.returnData)
      }
    })

  },
})